package com.shunda.ysdgj.manager.service.impl.goods;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.shunda.ysdgj.common.exception.CustomException;
import com.shunda.ysdgj.common.util.ValidateUtil;
import com.shunda.ysdgj.manager.mapper.goods.GoodsSpecCategoryMapper;
import com.shunda.ysdgj.manager.service.goods.GoodsSpecCategoryService;
import com.shunda.ysdgj.manager.service.goods.GoodsSpecService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.goods.GoodsSpecCategoryFindDto;
import com.shunda.ysdgj.model.entity.goods.GoodsSpecCategory;
import com.shunda.ysdgj.model.vo.ResultCodeEnum;
import com.shunda.ysdgj.model.vo.goods.GoodsSpecCategoryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GoodsSpecCategoryServiceImpl
        extends MPJBaseServiceImpl<GoodsSpecCategoryMapper, GoodsSpecCategory>
        implements GoodsSpecCategoryService, MPJBaseService<GoodsSpecCategory> {

    private final GoodsSpecService goodsSpecService;

    public List<GoodsSpecCategoryVo> findAll() {
        MPJLambdaWrapper<GoodsSpecCategory> baseWrapper = JoinWrappers
                .lambda(GoodsSpecCategory.class)
                .select(GoodsSpecCategory::getId, GoodsSpecCategory::getSpecCategoryName);
        return getBaseMapper().selectJoinList(GoodsSpecCategoryVo.class, baseWrapper);
    }

    public List<GoodsSpecCategoryVo> findByType(Integer categoryType) {
        MPJLambdaWrapper<GoodsSpecCategory> baseWrapper = JoinWrappers
                .lambda(GoodsSpecCategory.class)
                .select(GoodsSpecCategory::getId, GoodsSpecCategory::getSpecCategoryName)
                .eq(GoodsSpecCategory::getSpecCategoryType, categoryType);
        return getBaseMapper().selectJoinList(GoodsSpecCategoryVo.class, baseWrapper);
    }

    public IPage<GoodsSpecCategory> findByPage(GoodsSpecCategoryFindDto goodsSpecCategoryFindDto) {
        if (ValidateUtil.isNotLong(goodsSpecCategoryFindDto.getCurrent())) {
            goodsSpecCategoryFindDto.setCurrent(1L);
        }
        if (ValidateUtil.isNotLong(goodsSpecCategoryFindDto.getSize())) {
            goodsSpecCategoryFindDto.setSize(10L);
        }
        LambdaQueryChainWrapper<GoodsSpecCategory> lambdaQuery = lambdaQuery();
        if (ValidateUtil.isInteger(goodsSpecCategoryFindDto.getSpecCategoryType())) {
            lambdaQuery.eq(GoodsSpecCategory::getSpecCategoryType, goodsSpecCategoryFindDto.getSpecCategoryType());
        }
        if (StrUtil.isNotEmpty(goodsSpecCategoryFindDto.getSpecCategoryName())) {
            lambdaQuery.like(GoodsSpecCategory::getSpecCategoryName, goodsSpecCategoryFindDto.getSpecCategoryName());
        }
        IPage<GoodsSpecCategory> page = new Page<>(goodsSpecCategoryFindDto.getCurrent(), goodsSpecCategoryFindDto.getSize());
        return lambdaQuery.page(page);
    }

    public boolean add(GoodsSpecCategory goodsSpecCategory) {
        if (ValidateUtil.isBlank(goodsSpecCategory.getSpecCategoryName())
                || ValidateUtil.isNotInteger(goodsSpecCategory.getSpecCategoryType())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        Long count = lambdaQuery()
                .eq(GoodsSpecCategory::getSpecCategoryName, goodsSpecCategory.getSpecCategoryName())
                .count();
        if (count > 0) {
            throw new CustomException(ResultCodeEnum.GOODS_SPEC_CATEGORY_REPEAT);
        }
        goodsSpecCategory.setId(null);
        return save(goodsSpecCategory);
    }

    public boolean modify(GoodsSpecCategory goodsSpecCategory) {
        if (ValidateUtil.isNotLong(goodsSpecCategory.getId())
                || ValidateUtil.isBlank(goodsSpecCategory.getSpecCategoryName())
                || ValidateUtil.isNotInteger(goodsSpecCategory.getSpecCategoryType())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return updateById(goodsSpecCategory);
    }

    @Transactional
    public boolean deleteByIds(IdsDto idsDto) {
        List<Long> ids = idsDto.getIds();
        if (ids.isEmpty()) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        if (goodsSpecService.countByCategoryIds(ids) > 0) {
            throw new CustomException(ResultCodeEnum.GOODS_SPEC_CATEGORY_ERROR);
        }
        return removeByIds(ids);
    }

}
